<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="100px"
      v-show="showSearch"
    >
      <el-form-item label="优惠券名称" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入优惠券名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠券状态" prop="couponStatus">
        <el-select
          v-model="queryParams.couponStatus"
          placeholder="全部"
          clearable
          size="small"
        >
          <el-option
            v-for="item in dict.type.coupon_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优惠券类别" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="全部"
          clearable
          size="small"
        >
          <el-option
            v-for="item in dict.type.coupon_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入业务类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="适用项目类型" prop="useArea">
        <el-select
          v-model="queryParams.useArea"
          placeholder="全部"
          clearable
          size="small"
        >
          <el-option
            v-for="item in dict.type.coupon_useArea"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="适用单位类型" prop="applicableUnitType">
        <el-select
          v-model="queryParams.applicableUnitType"
          placeholder="全部"
          clearable
          size="small"
        >
          <el-option
            v-for="item in dict.type.coupon_applicableUnitType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['coupon:business:query']"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple.length === 0"
          @click="handleDelete(null)"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['coupon:business:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="businessList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column
        type="selection"
        width="55"
        align="center"
      /> -->
      <el-table-column
        min-width="160"
        label="优惠券名称"
        align="center"
        prop="couponName"
      />
      <el-table-column
        min-width="160"
        label="优惠券类别"
        align="center"
        prop="category"
      >
        <template slot-scope="scope">
          {{ scope.row.category | typeFilter(dict.type.coupon_type) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="优惠信息"
        align="center"
        prop="preferentialInfo"
      />
      <el-table-column
        min-width="180"
        label="使用条件"
        align="center"
        prop="useTimeType"
      >
        <template slot-scope="scope">
          {{
            scope.row.useTimeType | typeFilter(dict.type.coupon_use_time_type)
          }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="300"
        label="使用范围"
        align="center"
        prop="useTime"
      />
      <el-table-column
        min-width="160"
        label="优惠券状态"
        align="center"
        prop="couponStatus"
      >
        <template slot-scope="scope">
          {{ scope.row.couponStatus | typeFilter(dict.type.coupon_status) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="是否允许转赠"
        align="center"
        prop="isTurn"
      >
        <template slot-scope="scope">
          {{ scope.row.isTurn | typeFilter(dict.type.coupon_is_turn) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="优惠券开始时间"
        align="center"
        prop="sendTimeStart"
        sortable
      />
      <el-table-column
        min-width="160"
        label="优惠券结束时间"
        align="center"
        prop="sendTimeEnd"
      />
      <el-table-column
        min-width="160"
        label="所属租户"
        align="center"
        prop="tenantName"
      />
      <el-table-column
        min-width="160"
        label="创建时间"
        align="center"
        prop="createTime"
        sortable
      />
      <el-table-column
        min-width="160"
        label="创建人"
        align="center"
        prop="userName"
      />
      <el-table-column
        min-width="160"
        label="业务类型"
        align="center"
        prop="businessName"
      />
      <el-table-column
        min-width="160"
        label="使用类型"
        align="center"
        prop="useType"
      >
        <template slot-scope="scope">
          {{ scope.row.useType | typeFilter(dict.type.coupon_useType) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="使用类型名称"
        align="center"
        prop="useTypeName"
      />
      <el-table-column
        min-width="160"
        label="适用项目类型"
        align="center"
        prop="useArea"
      >
        <template slot-scope="scope">
          {{ scope.row.useArea | typeFilter(dict.type.coupon_useArea) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="指定项目名称"
        align="center"
        prop="serviceItems"
      >
        <template slot-scope="scope">
          {{ scope.row.useArea == 1 ? '' : scope.row.serviceItems }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="总库存"
        align="center"
        prop="sendTotal"
        sortable
      />
      <el-table-column
        min-width="160"
        label="已发放"
        align="center"
        prop="issued"
      />
      <el-table-column
        min-width="160"
        label="适用单位类型"
        align="center"
        prop="applicableUnitType"
      >
        <template slot-scope="scope">
          {{
            scope.row.applicableUnitType
              | typeFilter(dict.type.coupon_applicableUnitType)
          }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="适用单位名称"
        align="center"
        prop="applicableUnit"
      >
        <template slot-scope="scope">
          {{
            scope.row.applicableUnitType == 1 ? '' : scope.row.applicableUnit
          }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="发送节点名称"
        align="center"
        prop="distributedNode"
      />
      <el-table-column
        min-width="160"
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handDetail(scope.row)"
            >详情</el-button
          >
          <!-- 1 -->
          <el-dropdown
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleEdit"
                icon="el-icon-key"
                v-hasPermi="['coupon:business:edit']"
                >编辑</el-dropdown-item
              >
              <el-dropdown-item
                command="handleDelete"
                icon="el-icon-key"
                v-hasPermi="['coupon:business:remove']"
                >删除</el-dropdown-item
              >
              <el-dropdown-item
                command="handleActive"
                icon="el-icon-key"
                v-hasPermi="['coupon:business:active']"
                >活动数据</el-dropdown-item
              >
              <el-dropdown-item
                command="handleSendUser"
                icon="el-icon-circle-check"
                v-hasPermi="['coupon:business:send']"
                >发送指定用户</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="sendUser.title"
      :visible.sync="sendUser.open"
      width="50%"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        :model="sendUser.queryParams"
        ref="sendUserQueryForm"
        :inline="true"
        label-width="100px"
        v-show="showSearch"
      >
        <el-form-item label="客户名称" prop="memberName">
          <el-input
            v-model="sendUser.queryParams.memberName"
            placeholder="请输入客户名称"
            clearable
            size="small"
            @keyup.enter.native="searchHandleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="searchHandleQuery"
            >查询</el-button
          >
          <el-button
            icon="el-icon-refresh"
            size="mini"
            @click="sendUserResetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="sendUser.loading"
        :data="sendUser.list"
        @selection-change="sendUserSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          min-width="100"
          label="客户名称"
          align="center"
          prop="memberName"
        />
        <el-table-column
          min-width="80"
          label="微信头像"
          align="center"
          prop="vxUrl"
        >
          <template slot-scope="scope">
            <el-image
              style="width: 48px; height: 48px"
              :src="scope.row.vxUrl"
              fit="fill"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column
          min-width="100"
          label="微信昵称"
          align="center"
          prop="vxName"
        />
        <el-table-column
          min-width="140"
          label="客户联系方式"
          align="center"
          prop="memberPhone"
        />

        <!-- <el-table-column
          min-width="80"
          label="客户类型"
          align="center"
          prop="memberType"
        >
          <template slot-scope="scope">
            {{ scope.row.memberType | typeFilter(dict.type.coupon_memberType) }}
          </template>
        </el-table-column>
        <el-table-column
          min-width="140"
          label="手机型号"
          align="center"
          prop="phoneModel"
        /> -->
      </el-table>
      <pagination
        :total="sendUser.total"
        :page.sync="sendUser.queryParams.pageNum"
        :limit.sync="sendUser.queryParams.pageSize"
        @pagination="sendUserHandleQuery"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getBusinessList,
  exportBusiness,
  memberBusiness,
  deleteBusinessById,
  memberList,
} from '@/api/coupon/business.js'

export default {
  name: 'CouponBusiness',
  dicts: [
    'coupon_status',
    'coupon_type',
    'coupon_use_time_type',
    'coupon_is_turn',
    'coupon_useArea',
    'coupon_useType',
    'coupon_applicableUnitType',
    // 'coupon_memberType',
  ],
  data() {
    return {
      sendUser: {
        open: false,
        title: '发送指定客户',
        list: [],
        total: 0,
        sendList: {
          memberVoList: [],
          businessId: '',
          couponId: '',
          couponName: '',
        },
        queryParams: {
          memberName: '',
          pageNum: 1,
          pageSize: 10,
        },
      },
      showSearch: true,
      // 表格遮罩
      loading: false,
      // 表格数据
      businessList: [],
      total: 1,
      // 删除数据
      multiple: [],
      // 导出遮罩层
      exportLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponName: '',
        couponStatus: null,
        businessName: '',
        category: null,
        useArea: null,
        applicableUnitType: null,
      },
    }
  },
  filters: {
    typeFilter(val, type) {
      if (val === null) return null
      let data = type.filter((item) => {
        return val == item.value
      })
      if (data.length) {
        return data[0].label
      } else {
        return val
      }
    },
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询客户列表 */
    sendUserHandleQuery() {
      this.sendUser.loading = true
      memberList(this.sendUser.queryParams).then((response) => {
        this.sendUser.list = response.rows
        this.sendUser.total = response.total
        this.sendUser.loading = false
      })
    },
    // 选择指定用户数据
    sendUserSelectionChange(selection) {
      this.sendUser.sendList.memberVoList = selection
    },
    // 提交指定用户
    submitForm() {
      if (!this.sendUser.sendList.memberVoList.length) {
        this.$message.error('指定客户为空')
        return false
      }
      memberBusiness(this.sendUser.sendList).then((res) => {
        this.$message.success(res.msg)
        this.sendUser.open = false
        this.getList()
      })
    },
    // 取消按钮
    cancel() {
      this.sendUser.open = false
      this.resetForm('sendUserQueryForm')
    },
    /** 查询租户管理列表 */
    getList() {
      this.loading = true
      getBusinessList(this.queryParams).then((response) => {
        this.businessList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    searchHandleQuery() {
      this.sendUser.queryParams.pageNum = 1
      this.sendUserHandleQuery()
    },

    /** 重置按钮操作 */
    sendUserResetQuery() {
      this.resetForm('sendUserQueryForm')
      this.sendUserHandleQuery()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.multiple = selection.map((item) => item.businessId)
    },
    /** 详情按钮操作 */
    handDetail(row) {
      this.$router.push({ path: '/coupon/business/detail/' + row.businessId })
    },
    /** 操作事件 */
    handleCommand(val, row) {
      switch (val) {
        case 'handleActive':
          // 活动数据
          this.$router.push({
            path: '/coupon/business/active/' + row.businessId,
          })
          break
        case 'handleSendUser':
          // 发送指定客户
          this.sendUser.open = true
          this.sendUser.sendList.couponName = row.couponName
          this.sendUser.sendList.couponId = row.couponId
          this.sendUser.sendList.businessId = row.businessId
          this.sendUserHandleQuery()
          break
        case 'handleEdit':
          // 编辑
          this.$router.push({ path: '/coupon/business/edit/' + row.businessId })
          break
        case 'handleDelete':
          // 删除
          this.handleDelete(row)
          break
        default:
          console.log('操作类型不正确')
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = {
        couponName: this.queryParams.couponName,
        couponStatus: this.queryParams.couponStatus,
        category: this.queryParams.category,
        businessName: this.queryParams.businessName,
        useArea: this.queryParams.useArea,
        applicableUnitType: this.queryParams.applicableUnitType,
      }
      this.$modal
        .confirm('是否确认导出所有优惠券业务数据项？')
        .then(() => {
          this.exportLoading = true
          exportBusiness(queryParams)
            .then((res) => {
              this.$download.name(res.msg)
              this.exportLoading = false
            })
            .catch((err) => {
              this.exportLoading = false
              console.log(err)
            })
        })
        .catch(() => {})
    },
    /** 删除操作 */
    handleDelete(row) {
      let message, ids
      if (row) {
        message =
          '是否确认删除优惠券业务名称为"' + row.couponName + '的数据项？'
        ids = row.businessId
      } else {
        message = '是否确认删除优惠券业务数据项？'
        ids = this.multiple.join(',')
      }
      this.$modal.confirm(message).then((e) => {
        deleteBusinessById({ id: ids }).then((res) => {
          this.$message.success(res.msg)
          this.getList()
        })
      })
    },
  },
}
</script>
